package com.ruoyi.app.service;

import com.ruoyi.app.domain.bo.AppUserBo;
import com.ruoyi.app.domain.entity.AppUser;
import com.ruoyi.app.domain.vo.AppUserVo;
import com.ruoyi.app.mapper.AppUserMapper;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户管理Service业务层处理
 *
 * @author zcc
 * @date 2024-12-14
 */
@RequiredArgsConstructor
@Service
public class AppUserService {

    private final AppUserMapper appUserMapper;

    /**
     * 查询用户管理
     */
    public AppUserVo queryById(Long userId){
        return appUserMapper.selectVoById(userId);
    }

    /**
     * 查询用户管理列表
     */
    public TableDataInfo<AppUserVo> queryPageList(AppUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUser> lqw = buildQueryWrapper(bo);
        Page<AppUserVo> result = appUserMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户管理列表
     */
    public List<AppUserVo> queryList(AppUserBo bo) {
        LambdaQueryWrapper<AppUser> lqw = buildQueryWrapper(bo);
        return appUserMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUser> buildQueryWrapper(AppUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppUser> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), AppUser::getUserName, bo.getUserName());
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), AppUser::getNickName, bo.getNickName());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), AppUser::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getMobile()), AppUser::getMobile, bo.getMobile());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), AppUser::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), AppUser::getAvatar, bo.getAvatar());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), AppUser::getPassword, bo.getPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getSalt()), AppUser::getSalt, bo.getSalt());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AppUser::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getLoginIp()), AppUser::getLoginIp, bo.getLoginIp());
        lqw.eq(bo.getLoginDate() != null, AppUser::getLoginDate, bo.getLoginDate());
        return lqw;
    }

    /**
     * 新增用户管理
     */
    public void insertByBo(AppUserBo bo) {
        AppUser add = MapstructUtils.convert(bo, AppUser.class);
        appUserMapper.insert(add);
    }

    /**
     * 修改用户管理
     */
    public void updateByBo(AppUserBo bo) {
        AppUser update = MapstructUtils.convert(bo, AppUser.class);
        appUserMapper.updateById(update);
    }

    /**
     * 批量删除用户管理
     */
    public void deleteByIds(Collection<Long> ids) {
        appUserMapper.deleteBatchIds(ids);
    }
}
